/*
Valid Parentheses

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
*/

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <stack>
#include "print.h"
using namespace std;


void testForStack()
{
	stack<int> mystack;
	mystack.push(10);
	mystack.push(20);
	mystack.top() -= 5;
	cout << "mystack.top() is now " << mystack.top() << endl;
}

class Solution {
private:
	bool isPush(stack<string> &strStack, string &s, map<string, string> &strMap)
	{
		if (s == "(" || s == "[" || s == "{" || (strMap[s] != strStack.top()))
		{
			return true;
		}
		else if (strMap[s] == strStack.top())
		{
			return false;
		}
		
	}

public:
	bool isValid(string s) {
		
		stack<string> stackStr;
		map<string, string> strMap;
		strMap[")"] = "(";
		strMap["]"] = "[";
		strMap["}"] = "{";

		string temp;

		if (s.size() ==0 || s.size() & 0x01)
		{
			return false;
		}
		for (size_t i = 0; i < s.size(); i++)
		{
			temp = s[i];
			if (stackStr.size() == 0 || isPush(stackStr, temp, strMap))
			{
				stackStr.push(temp);
			}
			else 
			{
				stackStr.pop();
			}
			
		}
		//if (true)
		
		return (stackStr.size() == 0 ? true : false);

	
	}
};

int main(int argc, char* argv[])
{
	Solution s;
	string str;

	//testForStack();

	for (int i = 1; i < argc; i++){
		
		cout << argv[i] << endl;
		str = argv[i];

	}

	cout << endl;
	if (s.isValid(str))
	{
		cout << "The string is valid:" << endl;
	}
	else
	{
		cout << "The string is not valid:" << endl;
	}
	
	
	
	system("pause");
	return 0;
}